<template>
  <el-dialog
    :title="!dataForm.styleCode ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">

    <el-form-item label="name" prop="styleName">
      <el-input v-model="dataForm.styleName" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="url" prop="styleImageUrl">
<!--      <el-input v-model="dataForm.styleImageUrl" placeholder=""></el-input>-->
      <el-upload
        ref="clearData"
        class="upload-demo"
        drag
        :data="dataObj"
        :action="dataObj.host"
        :before-upload="getPolicy"
        :on-success="handleUploadSuccess"
        multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
      </el-upload>
    </el-form-item>
    <el-form-item label="desc" prop="styleDesc">
      <el-input v-model="dataForm.styleDesc" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="clearData" >取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          styleCode:'',
          styleName: '',
          styleImageUrl: '',
          styleDesc:'',
        },
        dataObj: {
          policy: '',
          signature: '',
          OSSAccessKeyId: '',
          key: '',
          dir: '',
          host: '',
          expire: ''
        },
        dataRule: {
          styleName: [
            { required: true, message: 'name不能为空', trigger: 'blur' }
          ],
          styleImageUrl: [
            { required: true, message: 'url不能为空', trigger: 'blur' }
          ],
          styleDesc: [
            { required: true, message: 'desc不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (styleCode) {
        this.dataForm.styleCode = styleCode || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.styleCode) {
            this.$http({
              url: this.$http.adornUrl(`/style/style/queryWormholeGalaxyStyle`),
              method: 'post',
              data: this.$http.adornData({
                "styleCode":styleCode
              })
            }).then(({data}) => {
              if (data) {
                this.dataForm.styleName = data.data.styleName
                this.dataForm.styleImageUrl = data.data.styleImageUrl
                this.dataForm.styleDesc = data.data.styleDesc
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/style/style/${!this.dataForm.styleCode ? 'createWormholeGalaxyStyle' : 'updateWormholeGalaxyStyle'}`),
              method: 'post',
              data: this.$http.adornData({
                'styleCode': this.dataForm.styleCode || '',
                'styleName': this.dataForm.styleName,
                'styleImageUrl': this.dataForm.styleImageUrl,
                'styleDesc': this.dataForm.styleDesc
              })
            }).then(({data}) => {
              if (data) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
                this.clearData();
              } else {
                this.$message.error(data.msg)
                this.clearData();
              }
            })
          }
        })
      },

      // 获取上传签名
      getPolicy(file) {
        return this.$http({
          url: this.$http.adornUrl('/oss/uploadByPolicy'),
          method: 'post',
          data: this.$http.adornData({
            "path": 'style',
          })
        }).then(({data}) => {
          if (data) {
            this.dataObj.key = data.data.dir+file.name
            this.dataObj.policy = data.data.policy
            this.dataObj.signature = data.data.signature
            this.dataObj.OSSAccessKeyId = data.data.accessKeyId
            this.dataObj.dir = data.data.dir
            this.dataObj.host = data.data.host
            this.dataObj.expire = data.data.expire
            // 添加1秒延迟
            return new Promise(resolve => setTimeout(() => resolve(true), 1000));
          }
        })
      },
      //上传成功处理
      handleUploadSuccess(response, file, fileList) {
        this.$message.success('文件上传成功！');
        this.dataForm.styleImageUrl = this.dataObj.host+"/"+this.dataObj.key
      },
    //清除表单数据:
      clearData () {
        this.dataForm.styleName = ''
        this.dataForm.styleImageUrl = ''
        this.dataForm.styleDesc = ''
        this.$refs.clearData.clearFiles()
      }

    }
  }
</script>
